package com.linkoog.devtools.intellij.ui.button;

import com.intellij.openapi.actionSystem.ActionButtonComponent;
import com.intellij.openapi.actionSystem.ex.ActionButtonLook;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class IconLabelButton extends JLabel {

    private boolean hovered = false;

    public IconLabelButton(Icon icon, ClickHandler onClickHandler) {
        super(icon);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) {
                    onClickHandler.onClick(IconLabelButton.this);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                hovered = true;
                IconLabelButton.this.repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                hovered = false;
                IconLabelButton.this.repaint();
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        if (hovered) {
            ActionButtonLook.SYSTEM_LOOK.paintBackground(g, this, ActionButtonComponent.SELECTED);
        }
        super.paintComponent(g);
    }

    public interface ClickHandler {
        void onClick(JComponent component);
    }
}
